*******************************************************************************
* Load candidates data
*******************************************************************************

use dataset_candidates, clear

*******************************************************************************
* Rescale ideology and presidential vote
*******************************************************************************

su ideology
replace ideology = (ideology - r(min))/(r(max) - r(min))
su trump
replace trump = (trump - r(min))/(r(max) - r(min))

*******************************************************************************
* For R3
*******************************************************************************

eststo clear
eststo: areg voteshare ideology cycle_* i.ncandidates if deputy==1, ///
	cl(district) a(state)
eststo: areg voteshare ideology cycle_* i.ncandidates if deputy==0, ///
	cl(district) a(state)
eststo: areg voteshare c.ideology##deputy cycle_* i.ncandidates, ///
	cl(district) a(state)
eststo: areg winner ideology cycle_* i.ncandidates if deputy==1, ///
	cl(district) a(state)
eststo: areg winner ideology cycle_* i.ncandidates if deputy==0, ///
	cl(district) a(state)
eststo: areg winner c.ideology##deputy cycle_* i.ncandidates, ///
	cl(district) a(state)
esttab, compress se keep(*ideology*)
	
*******************************************************************************
* Do the regressions with deputy, ideology, and incumbency
*******************************************************************************

eststo clear

eststo: areg voteshare deputy cycle_* i.ncandidates, cl(district) a(state)
eststo: areg voteshare deputy ideology cycle_* i.ncandidates, ///
	cl(district) a(state)
eststo: areg voteshare deputy ideology incumbent cycle_* i.ncandidates, ///
	cl(district) a(state)

eststo: areg winner deputy cycle_* i.ncandidates, cl(district) a(state)
eststo: areg winner deputy ideology cycle_* i.ncandidates, ///
	cl(district) a(state)
eststo: areg winner deputy ideology incumbent cycle_* i.ncandidates, ///
	cl(district) a(state)

*******************************************************************************
* Output
*******************************************************************************

label variable deputy "Experienced"
label variable incumbent "Incumbent"
label variable ideology "Candidate Conservatism"

#delimit;

esttab
	using "_output/table4_a.tex"
	,
		replace
		booktabs
		cells(b(fmt(3)) se(par) p(par("[" "]")))
		drop(cycle* *cand* _cons) 
		stats(
			N,
			label(
				"Observations"
			)
			fmt(%20.0f %20.0f)
		)
		label
		collabels(none)	
		mlabels(none)
		width(\linewidth)
		alignment(l)
		gap
		mgroups(
			"DV=Vote Share" "DV=Elected", pattern(1 0 0 1 0 0) 
			prefix(\multicolumn{@span}{c}{) suffix(}) 
			span 
			erepeat(\cmidrule(lr){@span})
		)
		;
		
#delimit cr

*******************************************************************************
* Make interactions
*******************************************************************************

gen deputyXtrumpvoteshare = deputy*trumpvoteshare
gen ideologyXtrumpvoteshare = ideology*trumpvoteshare

*******************************************************************************
* Do the regressions with deputy and ideology
*******************************************************************************

eststo clear

eststo: areg voteshare ideology trumpvoteshare ideologyXtrumpvoteshare cycle_* ///
	i.ncandidates, cl(district) a(state)	
eststo: areg voteshare deputy trumpvoteshare deputyXtrumpvoteshare cycle_* ///
	i.ncandidates, cl(district) a(state)

eststo: areg winner ideology trumpvoteshare ideologyXtrumpvoteshare cycle_* ///
	i.ncandidates, cl(district) a(state)	
eststo: areg winner deputy trumpvoteshare deputyXtrumpvoteshare cycle_* ///
	i.ncandidates, cl(district) a(state)

*******************************************************************************
* Output
*******************************************************************************

label variable deputy "Experienced"
label variable incumbent "Incumbent"
label variable trumpvoteshare "Trump Vote Share"
label variable deputyXtrumpvoteshare "Experienced X Trump Vote"
label variable ideologyXtrumpvoteshare "Cand. Conservatism X Trump Vote"

#delimit;

esttab
	using "_output/table4_b.tex"
	,
		replace
		booktabs
		cells(b(fmt(3)) se(par) p(par("[" "]")))
		order(deputy ideology)
		drop(cycle* *cand* _cons) 
		stats(
			N,
			label(
				"Observations"
			)
			fmt(%20.0f %20.0f)
		)
		label
		collabels(none)	
		mlabels(none)
		width(\linewidth)
		alignment(l)
		gap
		mgroups(
			"DV=Vote Share" "DV=Elected", pattern(1 0 1 0) 
			prefix(\multicolumn{@span}{c}{) suffix(}) 
			span 
			erepeat(\cmidrule(lr){@span})
		)
		;
		
#delimit cr

*******************************************************************************
* End
*******************************************************************************
